package com.yugao.fintech.antelope.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yugao.fintech.antelope.system.mapper.SysPositionMapper;
import com.yugao.fintech.antelope.system.model.entity.SysPosition;
import com.yugao.fintech.antelope.system.service.SysPositionService;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.page.PageModel;
import com.yugao.fintech.framework.core.page.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPositionServiceImpl implements SysPositionService {
    @Autowired
    private SysPositionMapper sysPositionMapper;

    @Override
    public List<SysPosition> listAll() {
        return sysPositionMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public IPage<SysPosition> pagePosition(SysPosition rq) {
        LambdaQueryWrapper<SysPosition> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty( rq.getName())) {
            queryWrapper.eq(SysPosition::getName, rq.getName());
        }
        PageModel pageDomain = TableSupport.getPageModel();
        return sysPositionMapper.selectPage(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()), queryWrapper);
    }

    @Override
    public SysPosition getById(Long id) {
        return sysPositionMapper.selectById(id);
    }

    @Override
    public void save(SysPosition sysPosition) {
        if (sysPositionMapper.insert(sysPosition) == 0) {
            throw new BizException("保存失败");
        }
    }

    @Override
    public void updateById(SysPosition sysPosition) {
        sysPositionMapper.updateById(sysPosition);
    }

    @Override
    public void deleteById(Long id) {
        sysPositionMapper.deleteById(id);
    }
}
